const koa = require("koa")
const koaBody = require("koa-bodyparser")
const { accse } = require('./config/accse')
const analysisModule = require('./utils/analysis-module')
const {servesRes} = require('./utils/servesRes')
const { userApi } = require("./user/getUserList")
const { menuApi } = require('./menu')
const KoaServers = new koa()

KoaServers.use(koaBody())

KoaServers.use(async (ctx, next) => {
  // 解决跨域问题及网络配置
  accse(ctx)
  if (ctx.method == 'OPTIONS') {
    ctx.body = 200;
  } else {
    await next();
  }
  // koa ctx参数中办函了req&res
  // 更加的智能不需要设置header数据类型，自动识别，直接给body赋值即可
  if (analysisModule(ctx.request.url, 'user')) await servesRes(ctx, async (net) => await userApi(net))
  if (analysisModule(ctx.request.url, 'menu')) await servesRes(ctx, async (net) => await menuApi(net))
})

KoaServers.listen(9000, () => {
  console.log('=================')
})
